package com.briup.day41;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ChatServer2 {
	public static void main(String[] args) {
		// 创建并开启服务器
		int port = 8888;
		try {
			ServerSocket ss = new ServerSocket(port);
			System.out.println("服务器已启动，端口号：" + port);
			System.out.println("等待客户端的连接....");
			// 进入侦听状态等待客户端的连接
			Socket socket = ss.accept();
			System.out.println("一台客户端已连接！");

			// 接收客户端发送的信息
			InputStream is = socket.getInputStream();
			byte[] buf = new byte[128];
			int len = -1;
			while ((len = is.read(buf)) != -1) {
				System.out.print(new String(buf, 0, len));
			}
			socket.shutdownInput();
			
			// 向客户端回复一条信息
			OutputStream os = socket.getOutputStream();
			Scanner sc = new Scanner(System.in);
			String msg = sc.nextLine();
			os.write(msg.getBytes());
			os.flush();
			socket.shutdownOutput();

			

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}
